package com.dmap.knowledge.controller.api;

import com.dmap.base.constants.Constant;
import com.dmap.base.vo.BaseVo;
import com.dmap.knowledge.po.Article;
import com.dmap.knowledge.service.ArticleColumnAppService;
import com.dmap.knowledge.service.ArticleColumnService;
import com.dmap.knowledge.service.ArticleExtensionService;
import com.dmap.knowledge.service.ArticleService;
import com.dmap.knowledge.vo.ArticleDetailVo;
import com.dmap.knowledge.vo.ArticleTotalityInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(value = "文章", description = "App/文章")
@RestController
@RequestMapping(value = "/api/dmap-knowledge-server/v1/article")
public class ArticleController {

    @Resource
    private ArticleColumnService articleColumnService;
    @Resource
    private ArticleColumnAppService articleColumnAppService;
    @Resource
    private ArticleExtensionService articleExtensionService;
    @Resource
    private ArticleService articleService;

    @ResponseBody
    @ApiOperation(value = "栏目文章", notes = "获取栏目文章")
    @RequestMapping(value = "/column/articleList", method = RequestMethod.GET)

    public BaseVo selectColumnArticleList(@RequestHeader(value = "language") String lang, @RequestParam String projectId, @RequestParam long userId, @RequestParam long columnId, @RequestParam Integer pageIndex, @RequestParam Integer pageSize) {
        BaseVo result = new BaseVo();
        if (lang == null || userId == 0 || projectId == null || pageIndex == null || pageSize == null || columnId == 0) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (StringUtils.isBlank(lang) || StringUtils.isBlank(projectId)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        List<ArticleDetailVo> articleDetailVos = articleExtensionService.selectByArticleColumnId(lang, projectId, columnId, pageIndex, pageSize);
        result.setData(articleDetailVos);
        return result;
    }


    @ResponseBody
    @ApiOperation(value = "热门推荐文章", notes = "获取热门推荐文章")
    @RequestMapping(value = "/hot/recommend", method = RequestMethod.GET)
    public BaseVo selectHotRecommend(@RequestHeader(value = "language") String lang, @RequestParam String projectId, @RequestParam long userId) {
        BaseVo result = new BaseVo();
        result.setCode(Constant.RESPONSE_SUCCESS_CODE);
        if (lang == null || projectId == null || userId == 0) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (StringUtils.isBlank(lang) || StringUtils.isBlank(projectId)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        List<ArticleDetailVo> articleDetailVos = articleExtensionService.selectHotArticle(lang, projectId);
        result.setData(articleDetailVos);
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "栏目标签和文章列表", notes = "获取栏目标签和文章列表")
    @RequestMapping(value = "/totality/info", method = RequestMethod.GET)
    public BaseVo selectTotalityInfo(@RequestHeader(value = "language", required = true) String lang, @RequestParam String projectId, @RequestParam long userId) {
        BaseVo result = new BaseVo();
        if (lang == null || projectId == null || userId == 0) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (StringUtils.isBlank(lang) || StringUtils.isBlank(projectId)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        //加载缓存
        ArticleTotalityInfoVo  articleTotalityInfoVo  =  articleExtensionService.updateArticleTotalityInfo(lang,projectId);
        result.setData(articleTotalityInfoVo);
        return result;
    }
    
    /**
     * 查看文章信息
     */
    @ResponseBody
    @ApiOperation(value = "查看文章信息", notes = "查看文章信息")
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public BaseVo selectArticle(@RequestParam String articleId) {
        BaseVo result = new BaseVo();
        if (articleId == null) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        //做非数字检测
        if (!StringUtils.isNumeric(articleId)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        Article article = articleService.selectByArticleId(Long.parseLong(articleId));
        
        if (article == null) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        ArticleDetailVo articleDetailVo = new ArticleDetailVo(article);
        articleDetailVo.setTags(articleService.getArticleTagNames(article.getId()));
        result.setData(articleDetailVo);
        return result;
    }
}
